#Regressions 
#Alan Yan
#September 25th, 2020

#### SETUP ####
#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse,
       DeclareDesign,
       stargazer,
       sandwich)

#load data
dt <- read_rds("01-data/clean-data")

#### ANALYSIS ####
#### *RPS COMPOSITE ####
#### **ATTITUDES ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_index ~ auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
              closeness_diff + stereotype + linked_fate_n + discblacks_n,
            data = .) -> model.rps.attitudes

# Adjust standard errors
vcovHC(model.rps.attitudes, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps.attitudes.se

#### **DEMOS ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_index ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n,
     data = .) -> model.rps.demos

# Adjust standard errors
vcovHC(model.rps.demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps.demos.se

#### **ATTITUDES AND DEMOS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_index ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n +
       r_ideology_n + auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
       closeness_diff + stereotype + linked_fate_n + discblacks_n,
     data = .) -> model.rps.attitudes_demos

# Adjust standard errors
vcovHC(model.rps.attitudes_demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps.attitudes_demos.se

#### *RPS ATTRIBUTION ####
#### **ATTITUDES ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_attribution ~ auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
       closeness_diff + stereotype + linked_fate_n + discblacks_n,
     data = .) -> model.rps_attribution.attitudes

# Adjust standard errors
vcovHC(model.rps_attribution.attitudes, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_attribution.attitudes.se

#### **DEMOS ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_attribution ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n + r_ideology_n,
     data = .) -> model.rps_attribution.demos

# Adjust standard errors
vcovHC(model.rps_attribution.demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_attribution.demos.se

#### **ATTITUDES AND DEMOS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_attribution ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n +
       r_ideology_n + auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
       closeness_diff + stereotype + linked_fate_n + discblacks_n,
     data = .) -> model.rps_attribution.attitudes_demos

# Adjust standard errors
vcovHC(model.rps_attribution.attitudes_demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_attribution.attitudes_demos.se

#### *RPS CONCERN ####
#### **ATTITUDES ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_concern ~  auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
       closeness_diff + stereotype + linked_fate_n + discblacks_n,
     data = .) -> model.rps_concern.attitudes

# Adjust standard errors
vcovHC(model.rps_concern.attitudes, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_concern.attitudes.se

#### **DEMOS ONLY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_concern ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n,
     data = .) -> model.rps_concern.demos

# Adjust standard errors
vcovHC(model.rps_concern.demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_concern.demos.se

#### **ATTITUDES AND DEMOS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(rps_concern ~ r_age_n + r_edu_n + r_income_n + r_sex + r_south + r_pid_n +
       r_ideology_n + auth_index + r_religious_imp_n + shame + anger + idimpt_n + 
       closeness_diff + stereotype + linked_fate_n + discblacks_n,
     data = .) -> model.rps_concern.attitudes_demos

# Adjust standard errors
vcovHC(model.rps_concern.attitudes_demos, type = "HC1") %>%
  diag() %>%
  sqrt() -> model.rps_concern.attitudes_demos.se

#### *PUNITIVE ATTITUDES AND RPS ####
#### **SAGGING PANTS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(saggingpants_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.sagging

#### **HOUSTON DRESS CODE  ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(houstonschool_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.houston

#### **BLM ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(blm_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.blm

#### **THREE STRIKES LAW ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(threestrikes_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.threestrikes

#### **DRUG LAWS ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(drugcrimes_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.drugs

#### **DEATH PENALTY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(deathpenalty_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.deathpenalty

#### **POLICE SHOOTING ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(justifiedshooting_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.shooting

#### ** WELFARE SPENDING####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(welfarespending_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.welfarespending

#### ** WELFARE WORK ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(welfarework_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.welfarework

#### ** TRUANCY ####
dt %>%
  filter(
    (gc == 1 | gc == 3) &
      attn_check == 1 
  ) %>%
  lm(truancy_n ~ r_ideology_n + r_age_n + r_income_n + r_edu_n + r_sex + r_south +
              rps_index + auth_index + r_pid_n + linked_fate_n,
            data = .) -> model.truancy

#### MAKING TABLES ####
#### RPS COMPOSITE TABLE ####
stargazer(
  model.rps.demos,
  model.rps.attitudes,
          model.rps.attitudes_demos,
          single.row = TRUE,
          style = "apsr",
          dep.var.labels = "Respectability Politics Scale (Composite)",
          covariate.labels = c("Age",
                               "Education",
                               "Income",
                               "Female",
                               "Residence in the South",
                               "Party ID (Democrat)",
                               "Ideology (Liberal)",
                               "Authoritarianism",
                               "Impt. of Religion to Life",
                               "In-group Shame",
                               "In-group Anger",
                               "Imp. of Black Id. to Self",
                               "Relative Closeness to Blacks",
                               "Endorsement of Neg. Stereotypes",
                               "Linked Fate", 
                               "Perceptions of Discrimination"),
  se = list(model.rps.demos.se,
            model.rps.attitudes.se,
            model.rps.attitudes_demos.se),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001))

#### RPS ATTRIBUTION TABLE ####
stargazer(
  model.rps_attribution.demos,
  model.rps_attribution.attitudes,
  model.rps_attribution.attitudes_demos,
  single.row = TRUE,
  style = "apsr",
  dep.var.labels = "Respectability Politics Scale (Attribution)",
  covariate.labels = c("Age",
                       "Education",
                       "Income",
                       "Female",
                       "Residence in the South",
                       "Party ID (Democrat)",
                       "Ideology (Liberal)",
                       "Authoritarianism",
                       "Impt. of Religion to Life",
                       "In-group Shame",
                       "In-group Anger",
                       "Imp. of Black Id. to Self",
                       "Relative Closeness to Blacks",
                       "Endorsement of Neg. Stereotypes",
                       "Linked Fate", 
                       "Perceptions of Discrimination"),
  se = list(model.rps_attribution.demos.se,
            model.rps_attribution.attitudes.se,
            model.rps_attribution.attitudes_demos.se),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001))

#### RPS CONCERN TABLE ####
stargazer(
  model.rps_concern.demos,
  model.rps_concern.attitudes,
  model.rps_concern.attitudes_demos,
  single.row = TRUE,
  style = "apsr",
  dep.var.labels = "Respectability Politics Scale (Concern)",
  covariate.labels = c("Age",
                       "Education",
                       "Income",
                       "Female",
                       "Residence in the South",
                       "Party ID (Democrat)",
                       "Ideology (Liberal)",
                       "Authoritarianism",
                       "Impt. of Religion to Life",
                       "In-group Shame",
                       "In-group Anger",
                       "Imp. of Black Id. to Self",
                       "Relative Closeness to Blacks",
                       "Endorsement of Neg. Stereotypes",
                       "Linked Fate", 
                       "Perceptions of Discrimination"),
  se = list(model.rps_concern.demos.se,
            model.rps_concern.attitudes.se,
            model.rps_concern.attitudes_demos.se),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001))

#### FIG 3 - TABLE FOR PART 1 ####
stargazer(
  model.sagging,
  model.houston,
  model.blm,
  model.threestrikes,
  style = "apsr",
  dep.var.labels = c("Sagging",
                     "Dress Code",
                     "BLM",
                     "Three strikes"),
  covariate.labels = c(
    "RPS",
    "Authoritarianism",
    "Party ID (Democrat)",
    "Linked Fate"
  ),
  omit = c("r_ideology_n",
           "r_age_n",
           "r_income_n",
           "r_edu_n",
           "r_sex",
           "r_south"),
  se = list(starprep(model.sagging, se_type = "HC2")[[1]],
            starprep(model.houston, se_type = "HC2")[[1]],
            starprep(model.blm, se_type = "HC2")[[1]],
            starprep(model.threestrikes, se_type = "HC2")[[1]]),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001),
  omit.stat = "f")

#### FIG 3 - TABLE FOR PART 2 ####
stargazer(
  model.drugs,
  model.deathpenalty,
  model.shooting,
  style = "apsr",
  dep.var.labels = c("Drug laws",
                     "Death penalty",
                     "Police shooting"),
  covariate.labels = c(
    "RPS",
    "Authoritarianism",
    "Party ID (Democrat)",
    "Linked Fate"
  ),
  omit = c("r_ideology_n",
           "r_age_n",
           "r_income_n",
           "r_edu_n",
           "r_sex",
           "r_south"),
  se = list(starprep(model.drugs, se_type = "HC2")[[1]],
            starprep(model.deathpenalty, se_type = "HC2")[[1]],
            starprep(model.shooting, se_type = "HC2")[[1]]),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001),
  omit.stat = "f")

#### FIG 3 - TABLE FORM PART 3 ####
stargazer(
  model.welfarespending,
  model.welfarework,
  model.truancy,
  style = "apsr",
  dep.var.labels = c(
                     "Welfare spending",
                     "Welfare work req.",
                     "Truancy"),
  covariate.labels = c(
    "RPS",
    "Authoritarianism",
    "Party ID (Democrat)",
    "Linked Fate"
  ),
  omit = c("r_ideology_n",
           "r_age_n",
           "r_income_n",
           "r_edu_n",
           "r_sex",
           "r_south"),
  se = list(starprep(model.welfarespending, se_type = "HC2")[[1]],
            starprep(model.welfarework, se_type = "HC2")[[1]],
            starprep(model.truancy, se_type = "HC2")[[1]]),
  align = TRUE,
  star.cutoffs = c(.05,.01,.001),
  omit.stat = "f")
